﻿using log4net;
using System;
using System.Text;

namespace JYH.Framework
{
    /// <summary>
    /// 日志类(Log4Net)
    /// </summary>
    public class Log4Helper
    {
        /// <summary>
        /// 实例化日志对象
        /// </summary>
        private static readonly ILog Log = LogManager.GetLogger("MyLogger");

        private static string msgHeader = "";

        /// <summary>
        /// 异常信息日志记录
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        private static string GetErrorCode(Exception ex)
        {
            if (ex == null)
                return "";
            var error = new StringBuilder();
            error.AppendLine(msgHeader + "Class Name :: " + ((ex.TargetSite == null || ex.TargetSite.DeclaringType == null) ? "null" : ex.TargetSite.DeclaringType.FullName));
            error.AppendLine("Method Name :: " + (ex.TargetSite == null ? "null" : ex.TargetSite.Name));
            error.AppendLine("Message :: " + (ex.Message ?? "null"));
            error.AppendLine("StatckTrace :: " + (ex.StackTrace ?? "null"));
            error.AppendLine("Source :: " + (ex.Source ?? "null"));
            error.AppendLine("InnerException :: " + (ex.InnerException == null ? "null" : ex.InnerException.ToString()));
            return error.ToString();
        }

        #region Debug日志

        /// <summary>
        /// Debug消息日志
        /// </summary>
        /// <param name="location">位置</param>
        /// <param name="msg">消息</param>
        public static void LogDebug(string location, string msg)
        {
            if (Log.IsDebugEnabled)
                Log.Debug(msgHeader + " :: " + location + " :: " + msg);
        }

        /// <summary>
        /// Debug消息日志
        /// </summary>
        /// <param name="location"></param>
        /// <param name="ex"></param>
        public static void LogDebug(string location, Exception ex)
        {
            if (Log.IsDebugEnabled)
                Log.Debug(location + " :: " + GetErrorCode(ex) + "\r\n" + GetErrorCode(ex.GetBaseException()));
        }

        #endregion

        #region Info日志

        /// <summary>
        /// Info消息日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        public static void LogInfo(string msg)
        {
            if (Log.IsInfoEnabled)
                Log.Info(msg);
        }

        /// <summary>
        /// Info消息日志
        /// </summary>
        /// <param name="location">日志记录位置</param>
        /// <param name="msg">日志内容</param>
        public static void LogInfo(string location, string msg)
        {
            if (Log.IsInfoEnabled)
                Log.Info(location + " :: " + msg);
        }

        /// <summary>
        /// Info消息日志
        /// </summary>
        /// <param name="ex">异常信息</param>
        public static void LogInfo(Exception ex)
        {
            if (Log.IsInfoEnabled)
                Log.Info(GetErrorCode(ex) + "\r\n" + GetErrorCode(ex.GetBaseException()));
        }

        /// <summary>
        /// Info消息日志
        /// </summary>
        /// <param name="location">日志记录位置</param>
        /// <param name="ex">异常信息</param>
        public static void LogInfo(string location, Exception ex)
        {
            if (Log.IsInfoEnabled)
                Log.Info(location + " :: " + GetErrorCode(ex) + "\r\n" + GetErrorCode(ex.GetBaseException()));
        }

        #endregion

        #region Error日志

        /// <summary>
        /// Error消息日志
        /// </summary>
        /// <param name="location">日志记录位置</param>
        /// <param name="msg">日志内容</param>
        public static void LogError(string location, string msg)
        {
            if (Log.IsErrorEnabled)
                Log.Error(msgHeader + " :: " + location + " :: " + msg);
        }

        /// <summary>
        /// Error消息日志
        /// </summary>
        /// <param name="location">日志记录位置</param>
        /// <param name="ex">异常信息</param>
        public static void LogError(string location, Exception ex)
        {
            if (Log.IsErrorEnabled)
                Log.Error(location + " :: " + GetErrorCode(ex) + "\r\n" + GetErrorCode(ex.GetBaseException()));
        }

        #endregion

        #region Fatal日志

        /// <summary>
        /// Fatal消息日志
        /// </summary>
        /// <param name="location">日志记录位置</param>
        /// <param name="msg">日志内容</param>
        public static void LogFatal(string location, string msg)
        {
            if (Log.IsFatalEnabled)
                Log.Fatal(msgHeader + " :: " + location + " :: " + msg);
        }

        /// <summary>
        /// Fatal消息日志
        /// </summary>
        /// <param name="location">日志记录位置</param>
        /// <param name="ex">异常信息</param>
        public static void LogFatal(string location, Exception ex)
        {
            if (Log.IsFatalEnabled)
                Log.Fatal(location + " :: " + GetErrorCode(ex) + "\r\n" + GetErrorCode(ex.GetBaseException()));
        }

        #endregion
    }
}
